﻿/********************************************************
 * 类名： InitReceiptBLL.cs
 * 功能： 收款单管理业务层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2014-02-08	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using PSS.DAL.FundManage;
using PSS.Entity.FundManage;
using PSS.Utility;

namespace PSS.BLL.FundManage
{
    public class InitReceiptBLL
    {
        private readonly InitReceiptDAL _dal = new InitReceiptDAL();

        public string GetInitReceiptList(InitReceiptQcEntity qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetInitReceiptList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetInitReceiptInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetInitReceiptInfo(id));
        }

        public string GetInitReceiptDetailList(InitReceiptDetailQcEntity qcmodel)
        {
            var list = _dal.GetInitReceiptDetailList(qcmodel);
            return JsonHelper.Encode(list);
        }

        public void AddInitReceipt(string data)
        {
            var model = new InitReceiptEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<InitReceiptDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.InitReceiptId = Guid.NewGuid();
                model.CustomerId = new Guid(row["CustomerId"].ToString());
                model.ReceiptSingleDate = Convert.ToDateTime(row["ReceiptSingleDate"].ToString());
                model.ReceiptSingleNo = row["ReceiptSingleNo"].ToString();
                model.Remark = row["Remark"] != null ? row["Remark"].ToString() : string.Empty;
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {

                    var cmodel = new InitReceiptDetailEntity
                                     {
                                         ReceiptDetailId = Guid.NewGuid(),
                                         InitReceiptId = model.InitReceiptId,
                                         AccountId = new Guid(item["AccountId"].ToString()),
                                         ReceivableMoney =
                                             item["ReceivableMoney"] != null
                                                 ? Convert.ToDecimal(item["ReceivableMoney"])
                                                 : 0,
                                         SCategoryId = new Guid(item["SCategoryId"].ToString()),
                                         SettleNo = item["SettleNo"].ToString(),
                                         Remark = item["Remark"].ToString()
                                     };
                    list.Add(cmodel);
                }
            }
            if (!_dal.AddInitReceipt(model)) return;
            _dal.AddInitReceiptDetail(list);
        }

        public void ModifyInitReceipt(string data)
        {
            var model = new InitReceiptEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<InitReceiptDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.InitReceiptId = new Guid(row["InitReceiptId"].ToString());
                model.CustomerId = new Guid(row["CustomerId"].ToString());
                model.ReceiptSingleDate = Convert.ToDateTime(row["ReceiptSingleDate"].ToString());
                model.ReceiptSingleNo = row["ReceiptSingleNo"].ToString();
                model.Remark = row["Remark"] != null ? row["Remark"].ToString() : string.Empty;
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {

                    var cmodel = new InitReceiptDetailEntity
                    {
                        ReceiptDetailId = Guid.NewGuid(),
                        InitReceiptId = model.InitReceiptId,
                        AccountId = new Guid(item["AccountId"].ToString()),
                        ReceivableMoney =
                            item["ReceivableMoney"] != null
                                ? Convert.ToDecimal(item["ReceivableMoney"])
                                : 0,
                        SCategoryId = new Guid(item["SCategoryId"].ToString()),
                        SettleNo = item["SettleNo"].ToString(),
                        Remark = item["Remark"].ToString()
                    };
                    list.Add(cmodel);
                }
            }
            if (!_dal.ModifyInitReceipt(model)) return;

            _dal.AddInitReceiptDetail(list);
        }

        public bool RemoveInitReceipt(string id)
        {
            _dal.RemoveInitReceiptDetail(id);
            return _dal.RemoveInitReceipt(id);
        }
    }
}
